﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using QLBH.DAO;
using QLBH.DTO;
using QLBH.BLO;

namespace QLBH
{
    public partial class HopDong : UserControl
    {
        string fileName;
        BLO_KhachHang blo_kh = new BLO_KhachHang();
        BLO_TTTai blo_tttai = new BLO_TTTai();
        BLO_TTTToan blo_tttt = new BLO_TTTToan();
        BLO_NghiepVu blo_ngv = new BLO_NghiepVu();
        BLO_NhanVien blo_nv = new BLO_NhanVien();
        BLO_QuanLy blo_ql = new BLO_QuanLy();
        BLO_TPA blo_tpa = new BLO_TPA();

        public HopDong()
        {
            InitializeComponent();
        }

        private void btnChonfile_Click(object sender, EventArgs e)
        {

            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "Excel Files|*.xls";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                fileName = dlg.FileName;
                txtTenFileVao.Text = dlg.FileName;
            }

        }

        private void btnThemvao_Click(object sender, EventArgs e)
        {
            try
            {
                DAO_HopDong hd = new DAO_HopDong(fileName);
                int from= int.Parse(txtNhapTuDong.Text.Trim());
                DataTable dt = hd.GetData(txtTenTrang.Text.Trim(),(from-1).ToString(), txtNhapDenDong.Text.Trim());
                //dataGridView1.DataSource = dt.DefaultView;
                foreach (DataRow row in dt.Rows)
                {
                    
                    //Them khach hang
                    KhachHang kh = new KhachHang();
                    kh.Tenkh = row[1].ToString();
                    kh.Ten_vt = row[2].ToString();
                    kh.Nhomkh = row[3].ToString();
                    blo_kh.Insert(kh);
                    //Them thong tin tai
                    TTTai tttai = new TTTai();
                    tttai.Loaihinhbh = row[21].ToString();
                    tttai.Nganhnghekd = row[22].ToString();
                    tttai.Tongmuctrachnhiem = row[23].ToString();
                    tttai.Songuoithamgia = row[24].ToString();
                    tttai.Taicodinh = row[25].ToString();
                    tttai.Taitamthoi = row[26].ToString();
                    blo_tttai.Insert(tttai);
                    //Them thong tin thanh toan
                    TTTT tttt = new TTTT();
                    tttt.Hanttky1 = row[27].ToString();
                    tttt.Phittky1 = row[28].ToString();
                    tttt.Hanttky2 = row[29].ToString();
                    tttt.Phittky2 = row[30].ToString();
                    tttt.Hanttky3 = row[31].ToString();
                    tttt.Phittky3 = row[32].ToString();
                    tttt.Hanttky4 = row[33].ToString();
                    tttt.Phittky4 = row[34].ToString();
                    tttt.Hanttky5 = row[35].ToString();
                    tttt.Phittky5 = row[36].ToString();
                    tttt.Ghichu = row[37].ToString();
                    blo_tttt.Insert(tttt);
                    //Them nghiep vu
                    NghiepVu nv = new NghiepVu();
                    nv.Ma_ngv = row[19].ToString();
                    nv.Ten_ngv = row[18].ToString();
                    nv.Nguoithan = row[20].ToString();
                    blo_ngv.Insert(nv);
                    //Them nhan vien
                    Phong mg = new Phong();
                    mg.Maphong = "P_MG";
                    Phong pti = new Phong();
                    pti.Maphong = "P_PTI";

                    NhanVien nvMg = new NhanVien();
                    nvMg.Ten_nv = row[12].ToString();
                    nvMg.Ma_phong = mg.Maphong;
                    blo_nv.Insert(nvMg, mg);
                   
                        
                    NhanVien tpMg = new NhanVien();
                    tpMg.Ten_nv = row[13].ToString();
                    tpMg.Truongphong = "TP";
                    tpMg.Ma_phong = mg.Maphong;
                    blo_nv.Insert(tpMg, mg);

                    NhanVien nvpti = new NhanVien();
                    nvpti.Ten_nv = row[15].ToString();
                    nvpti.Ma_phong = pti.Maphong;
                    blo_nv.Insert(nvpti, pti);

                    NhanVien tppti = new NhanVien();
                    tppti.Ten_nv = row[16].ToString();
                    tppti.Truongphong = "TP";
                    tppti.Ma_phong = pti.Maphong;
                    blo_nv.Insert(tppti, pti);
                    //Them TPA
                    TPA tpa= new TPA();
                    tpa.Ten_TPA=row[17].ToString();
                    blo_tpa.Insert(tpa);
                    //Them vao bang QUan Ly
                    QuanLy ql= new QuanLy();
                    ql.Kenh_kt=row[11].ToString();
                    ql.Tl_mgp=row[14].ToString();

                    blo_ql.Insert(ql, nvMg, tpMg, nvpti, tppti, nv, tpa);




                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
               // MessageBox.Show("Thêm thành công");
            }
        }
    }
}
